#include <sdk.h>
#include "configmanager.h"
#include "manager.h"
#include "ThreadSearchView.h"
#include "ThreadSearchViewManagerMessagesNotebook.h"

ThreadSearchViewManagerMessagesNotebook::~ThreadSearchViewManagerMessagesNotebook() {
}

void ThreadSearchViewManagerMessagesNotebook::AddViewToManager() {
  if( m_IsManaged == false ) {
    wxString prefix = ConfigManager::GetDataFolder() + _T( "/images/findf.png" );
    wxBitmap * bmp = new wxBitmap( M_Code_LoadBitmap( prefix, wxBITMAP_TYPE_PNG ) );
    CodeBlocksLogEvent evtShow( cbEVT_ADD_LOG_WINDOW, m_pThreadSearchView, wxString( _T( "搜索" ) ), bmp );
    Manager::Get()->ProcessEvent( evtShow );
    CodeBlocksLogEvent evtSwitch( cbEVT_SWITCH_TO_LOG_WINDOW, m_pThreadSearchView );
    Manager::Get()->ProcessEvent( evtSwitch );
    m_IsManaged = true;
    m_IsShown   = true;
  }
}

void ThreadSearchViewManagerMessagesNotebook::RemoveViewFromManager() {
  if( m_IsManaged == true ) {
    m_IsManaged = false;
    m_IsShown   = false;
    CodeBlocksLogEvent evt( cbEVT_REMOVE_LOG_WINDOW, m_pThreadSearchView );
    Manager::Get()->ProcessEvent( evt );
    m_pThreadSearchView->Reparent( Manager::Get()->GetAppWindow() );
    m_pThreadSearchView->Show( false );
  }
}

bool ThreadSearchViewManagerMessagesNotebook::ShowView( bool show ) {
  if( show == IsViewShown() ) {
    return false;
  }
  if( show == true ) {
    if( m_IsManaged == true ) {
      CodeBlocksLogEvent evtShow( cbEVT_SHOW_LOG_MANAGER );
      Manager::Get()->ProcessEvent( evtShow );
      CodeBlocksLogEvent evtSwitch( cbEVT_SWITCH_TO_LOG_WINDOW, m_pThreadSearchView );
      Manager::Get()->ProcessEvent( evtSwitch );
      m_IsShown = true;
    } else
    { AddViewToManager(); }
  } else {
    RemoveViewFromManager();
  }
  return true;
}

bool ThreadSearchViewManagerMessagesNotebook::IsViewShown() {
  return m_IsShown && IsWindowReallyShown( ( wxWindow* )m_pThreadSearchView );
}

void ThreadSearchViewManagerMessagesNotebook::Raise() {
  m_pThreadSearchView->GetParent()->GetParent()->Raise();
}
